{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "source": [
        "import pandas as pd\n",
        "from geopy.distance import geodesic\n",
        "\n",
        "df = pd.read_csv(\"fata_with_coords.csv\")\n",
        "\n",
        "def find_nearest_different_village(row, df):\n",
        "    min_dist = float('inf')\n",
        "    nearest_village = None\n",
        "    for index, other_row in df.iterrows():\n",
        "        if row.name != index and row['max_tribe_vill'] != other_row['max_tribe_vill']:\n",
        "            # Check if latitude and longitude are valid before calculating distance\n",
        "            if pd.notna(row['latitude']) and pd.notna(row['longitude']) and pd.notna(other_row['latitude']) and pd.notna(other_row['longitude']):\n",
        "                dist = geodesic((row['latitude'], row['longitude']), (other_row['latitude'], other_row['longitude'])).km\n",
        "                if dist < min_dist:\n",
        "                    min_dist = dist\n",
        "                    nearest_village = other_row.name  # Store index of nearest village\n",
        "    return min_dist, nearest_village\n",
        "\n",
        "df[['nearest_diff_village_dist', 'nearest_diff_village_index']] = df.apply(lambda row: pd.Series(find_nearest_different_village(row, df)), axis=1)\n",
        "\n",
        "df.to_csv(\"fata_with_distances.csv\", index=False)"
      ],
      "cell_type": "code",
      "metadata": {
        "id": "W_dyAGwEn7dz"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "df = pd.read_excel(\"fata_data.xlsx\")\n",
        "\n",
        "def calculate_ihi(series):\n",
        "    \"\"\"Calculates the Inverse Herfindahl Index for a given series.\"\"\"\n",
        "    # Get value counts and normalize to get probabilities\n",
        "    probs = series.value_counts(normalize=True)\n",
        "\n",
        "    # Calculate the squared sum of probabilities (Herfindahl Index)\n",
        "    hhi = (probs**2).sum()\n",
        "\n",
        "    # Calculate the IHI (1 / HHI)\n",
        "    ihi = 1 / hhi\n",
        "\n",
        "    return ihi\n",
        "\n",
        "# Group data by village code\n",
        "grouped = df.groupby('vill_code')\n",
        "\n",
        "# Calculate IHI for each variable within each village\n",
        "df['education_ihi'] = grouped['education'].transform(calculate_ihi)\n",
        "df['age_ihi'] = grouped['age'].transform(calculate_ihi)\n",
        "df['wealth_ihi'] = grouped['mont_incm'].transform(calculate_ihi)\n",
        "\n",
        "df.to_excel(\"fata_data_with_dispersion.xlsx\", index=False)"
      ],
      "cell_type": "code",
      "metadata": {
        "id": "s1lLo5JXcKD9"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "source": [
        "import folium\n",
        "import pandas as pd\n",
        "from folium.plugins import HeatMap\n",
        "\n",
        "# Load your data\n",
        "df = pd.read_csv(\"fata_with_distances.csv\")\n",
        "\n",
        "# Drop rows with NaN values in latitude or longitude\n",
        "df = df.dropna(subset=['latitude', 'longitude'])\n",
        "\n",
        "# Create a base map centered around the mean coordinates\n",
        "center_lat = df['latitude'].mean()\n",
        "center_lon = df['longitude'].mean()\n",
        "m = folium.Map(location=[center_lat, center_lon], zoom_start=8)\n",
        "\n",
        "# Create a heatmap layer using \"tribal_frac0\"\n",
        "heat_data = [[row['latitude'], row['longitude'], row['tribal_frac0']] for index, row in df.iterrows()]\n",
        "HeatMap(heat_data, radius=25, blur=15).add_to(m)\n",
        "\n",
        "# Create buffer zones (circles) around coordinates\n",
        "for index, row in df.iterrows():\n",
        "    folium.Circle(\n",
        "        location=[row['latitude'], row['longitude']],\n",
        "        radius=1000,  # Radius in meters (1 km)\n",
        "        color='blue',\n",
        "        fill=True,\n",
        "        fill_color='blue',\n",
        "        fill_opacity=0.2\n",
        "    ).add_to(m)\n",
        "\n",
        "# Save the map to an HTML file\n",
        "m.save(\"heatmap_with_buffers.html\")"
      ],
      "cell_type": "code",
      "metadata": {
        "id": "0ejCzDa92GIY"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import folium\n",
        "import pandas as pd\n",
        "from folium.plugins import HeatMap\n",
        "\n",
        "# Load your data\n",
        "df = pd.read_csv(\"fata_cseg.csv\")\n",
        "\n",
        "# Drop rows with NaN values in latitude or longitude\n",
        "df = df.dropna(subset=['latitude', 'longitude'])\n",
        "\n",
        "# Create a base map centered around the mean coordinates\n",
        "center_lat = df['latitude'].mean()\n",
        "center_lon = df['longitude'].mean()\n",
        "m = folium.Map(location=[center_lat, center_lon], zoom_start=8)\n",
        "\n",
        "# Create a heatmap layer using \"tribal_frac0\"\n",
        "heat_data = [[row['latitude'], row['longitude'], row['Cseg']] for index, row in df.iterrows()]\n",
        "HeatMap(heat_data, radius=25, blur=15).add_to(m)\n",
        "\n",
        "# Create buffer zones (circles) around coordinates\n",
        "for index, row in df.iterrows():\n",
        "    folium.Circle(\n",
        "        location=[row['latitude'], row['longitude']],\n",
        "        radius=1000,  # Radius in meters (1 km)\n",
        "        color='blue',\n",
        "        fill=True,\n",
        "        fill_color='blue',\n",
        "        fill_opacity=0.2\n",
        "    ).add_to(m)\n",
        "\n",
        "# Save the map to an HTML file\n",
        "m.save(\"heatmap_with_buffers2.html\")"
      ],
      "metadata": {
        "id": "QSa8XIQ73Hk7"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "pJfyZCVIkpaY"
      }
    }
  ]
}